package com.blockmeta.bbs.immodule.fragment;


import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseFragment;
import com.blockmeta.bbs.businesslibrary.decoration.GridSpaceDecoration;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.adapter.KeyboardCoinAndPairAdapter;
import com.blockmeta.bbs.immodule.bean.databean.F10ContentBean;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.net.api.IMNetApi;
import com.blockmeta.bbs.immodule.net.pojo.KeyboardCoinAndPairPOJO;
import com.blockmeta.bbs.immodule.widget.chatboard.BaseChatBoard;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class SendF10Fragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R2.id.rcv_list)
    RecyclerView mList;

    private KeyboardCoinAndPairAdapter mCoinAndPairAdapter;

    protected BaseChatBoard.OnOutPutListener mOnOutPutListener;

    private List<KeyboardCoinAndPairPOJO> mCoinAndPairPOJOList = new ArrayList<>();
    @Override
    public View setContentView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_send_f10, container, false);
    }

    @Override
    public void initView() {
        mList.setLayoutManager(new GridLayoutManager(mContext,4));
        mCoinAndPairAdapter  = new KeyboardCoinAndPairAdapter(R.layout.item_keyboard_coin_pair,mCoinAndPairPOJOList);
        GridSpaceDecoration decoration = new GridSpaceDecoration();
        decoration.setPx(DimentionsUtil.dp2px(10));
        mList.addItemDecoration(decoration);
        mList.setAdapter(mCoinAndPairAdapter);
        mCoinAndPairAdapter.setOnItemClickListener(this);

        List<Fragment> list = getFragmentManager().getFragments();
        for (Fragment fragment : list) {
            if (fragment instanceof BaseChatBoard) {
                mOnOutPutListener = ((BaseChatBoard)fragment).getOnOutPutListener();
                break;
            }
        }
        netWork();
    }

    private void netWork() {
        RetrofitFactory.getRetrofit().create(IMNetApi.ImKeyboardF10.class)
                .getKeyboardF10()
                .compose(this.<BaseCodeResponse<List<KeyboardCoinAndPairPOJO>>>bindToLifecycle())
                .compose(RxUtil.<BaseCodeResponse<List<KeyboardCoinAndPairPOJO>>>rxSchedulerHelper())
                .compose(RxUtil.<List<KeyboardCoinAndPairPOJO>>commonResultByCode())
                .subscribe(new CommonSubscriber<List<KeyboardCoinAndPairPOJO>>(mContext) {
                    @Override
                    public void onNext(List<KeyboardCoinAndPairPOJO> keyboardCoinAndPairPOJOS) {
                        mCoinAndPairPOJOList.clear();
                        int size = keyboardCoinAndPairPOJOS.size();
                        if (size>1){
                            if ((size&3)==0) {
                                keyboardCoinAndPairPOJOS.get(size - 1).name = null;
                            }else {
                                keyboardCoinAndPairPOJOS.add(new KeyboardCoinAndPairPOJO());
                            }
                        }
                        mCoinAndPairPOJOList.addAll(keyboardCoinAndPairPOJOS);
                        mCoinAndPairAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        KeyboardCoinAndPairPOJO coinAndPairPOJO = mCoinAndPairPOJOList.get(position);
        if (TextUtils.isEmpty(coinAndPairPOJO.name)){
            //跳转搜索更多
            if (mOnOutPutListener == null){return;}
            mOnOutPutListener.onOutPutText(ChatMessage.CONTENT_TYPE_CUSTOM,ChatMessage.CUSTOM_TYPE_F10,null);
            return;
        }
        F10ContentBean contentBean = new F10ContentBean();
        contentBean.setLogo(coinAndPairPOJO.logo);
        contentBean.setCoin_key(coinAndPairPOJO.coinKey);
        contentBean.setDescription(coinAndPairPOJO.describetion);
        contentBean.setCoin_name(coinAndPairPOJO.coinName);
        contentBean.setSymbol(coinAndPairPOJO.name);
        contentBean.setCn_name(coinAndPairPOJO.cnName);
        if(mOnOutPutListener != null){
            mOnOutPutListener.onOutPutText(ChatMessage.CONTENT_TYPE_CUSTOM, ChatMessage.CUSTOM_TYPE_F10, contentBean.toJsonString());
        }
    }
}
